package com.bw.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.bw.finance.conf.ResponseResult;
import com.bw.finance.eneity.TbRole;
import com.bw.finance.service.ITbRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhengqian
 * @since 2020-05-09
 */
@RestController
@RequestMapping("role")
public class TbRoleController {
    @Autowired
    private ITbRoleService service;

    @GetMapping("/getrole")
    public ResponseResult rolelist(){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            List<TbRole> list = service.list();
            result.setResult(list);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return result;
    }
    @PostMapping("/list/{pagenum}/{pagesize}")
    public ResponseResult list(@PathVariable Integer pagenum, @PathVariable Integer pagesize, @RequestBody TbRole tbRole){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            if(tbRole.getRoleName()!=null&&!"".equals(tbRole.getRoleName())){
                queryWrapper.like("ROLE_NAME","%"+tbRole.getRoleName()+"%");
            }
            PageHelper.startPage(pagenum,pagesize);
            List<TbRole> list = service.list(queryWrapper);
            PageInfo pageInfo = new PageInfo(list);
            result.setResult(pageInfo);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return result;
    }
    @PostMapping("/saverole")
    public ResponseResult saveuser(@RequestBody TbRole tbRole){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("ROLE_NAME",tbRole.getRoleName());
            List<TbRole> list = service.list(queryWrapper);
            if(list.size()>0&&null==tbRole.getId()){//如果查询用户名存在则提示用户
                result.setSuccess(false);
                result.setMessage("保存失败:用户名已存在");
                return result;
            }
            service.saveOrUpdate(tbRole);
            result.setMessage("保存成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return result;
    }
    @GetMapping("/getrole/{id}")
    public ResponseResult getuser(@PathVariable Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            TbRole tbRole = service.getById(id);
            result.setResult(tbRole);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("获取失败"+e.toString());
        }
        return result;
    }
}
